// 全局渠道管理工具
// 所有页面的渠道筛选都通过这个工具从ChannelManagement.vue获取数据

class ChannelManager {
  constructor() {
    this.channels = []
    this.listeners = []
    this.isInitialized = false
    
    // 初始化默认数据
    this.initDefaultChannels()
  }

  // 初始化默认渠道数据
  initDefaultChannels() {
    if (this.channels.length === 0) {
      this.channels = [
        { value: 0, name: '全部渠道', description: '用于查询所有渠道数据' },
        { value: 1, name: '官方渠道', description: '官方渠道' },
        { value: 2, name: '合作渠道A', description: '合作渠道A' },
        { value: 3, name: '合作渠道B', description: '合作渠道B' },
        { value: 4, name: '推广渠道', description: '推广渠道' },
        { value: 5, name: '内测渠道', description: '内测渠道' },
        { value: 6, name: '测试渠道', description: '测试渠道' },
      ]
      console.log('ChannelManager: 初始化默认渠道数据', this.channels)
    }
  }

  // 设置渠道数据（由ChannelManagement.vue调用）
  setChannels(channels) {
    if (channels && Array.isArray(channels) && channels.length > 0) {
      this.channels = channels
      this.isInitialized = true
      console.log('ChannelManager: 设置渠道数据', this.channels)
      // 通知所有监听者数据已更新
      this.notifyListeners()
    } else {
      console.warn('ChannelManager: 尝试设置空的渠道数据，使用默认数据')
      this.initDefaultChannels()
      this.notifyListeners()
    }
  }

  // 通知所有监听者
  notifyListeners() {
    this.listeners.forEach((callback, index) => {
      try {
        callback(this.channels)
      } catch (error) {
        console.error(`ChannelManager: 监听者回调执行失败 (index: ${index}):`, error)
      }
    })
  }

  // 获取渠道数据
  getChannels() {
    if (this.channels.length === 0) {
      this.initDefaultChannels()
    }
    return this.channels
  }

  // 获取渠道选项（用于下拉选择器）
  getChannelOptions() {
    const channels = this.getChannels()
    return channels.map(channel => ({
      label: channel.name,
      value: channel.value
    }))
  }

  // 根据value获取name
  getChannelName(value) {
    const channel = this.channels.find(ch => ch.value === value)
    return channel ? channel.name : value
  }

  // 根据name获取value
  getChannelValue(name) {
    const channel = this.channels.find(ch => ch.name === name)
    return channel ? channel.value : null
  }

  // 监听渠道数据变化
  subscribe(callback) {
    if (typeof callback === 'function') {
      this.listeners.push(callback)
      console.log('ChannelManager: 添加监听者，当前监听者数量:', this.listeners.length)
      
      // 如果已有数据，立即调用回调
      if (this.channels.length > 0) {
        try {
          callback(this.channels)
        } catch (error) {
          console.error('ChannelManager: 初始回调执行失败:', error)
        }
      }
    } else {
      console.error('ChannelManager: 订阅回调必须是函数')
    }
  }

  // 取消监听
  unsubscribe(callback) {
    const index = this.listeners.indexOf(callback)
    if (index > -1) {
      this.listeners.splice(index, 1)
      console.log('ChannelManager: 移除监听者，当前监听者数量:', this.listeners.length)
    }
  }

  // 获取状态信息
  getStatus() {
    return {
      channelCount: this.channels.length,
      listenerCount: this.listeners.length,
      isInitialized: this.isInitialized
    }
  }
}

// 创建全局实例
const channelManager = new ChannelManager()

export default channelManager 